<?php
class Bet2_3d
{

    public $table_name;
    public $db;
    public $tbl;
    public $gametype;

    public function Bet2_3d ($gametype = 1, $x = "")
    {
        global $g_gametype_3d;
        $this->table_name = $x . "bet2_" . $g_gametype_3d[$gametype]['en'];
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->gametype = $gametype;
        $this->db = new DB();
    }

    public function getMyBetRS ($id, $lotto_id)
    {
        $sql = "SELECT * FROM " . $this->table_name;
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id = {$lotto_id}";
        $sql .= " ORDER BY createtime desc, bettype, numbers";
        return $this->db->query($sql);
    }

    public function getSumAmount ($id, $lotto_id, $paperOpen = "")
    {
        $reVal = 0;
        $othrTbl = "";
        switch ($this->gametype)
        {
            case 1 :
                $othrTbl = "_3t";
                break;
            case 2 :
                $othrTbl = "_3d";
                break;
        }
        $sql = "!!SELECT sum(amount) as amount FROM {$this->table_name}";
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id = {$lotto_id}";
        $sql .= " AND rame IN (0,9)";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) as amount FROM bet{$othrTbl} a";
        $sql .= " LEFT JOIN lotto{$othrTbl} b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open = '{$paperOpen}'";
        $sql .= " AND a.rame IN (0,9)";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        return $reVal;
    }

    public function getSumAmountAllMem ($ids, $lotto_id)
    {
        $reVal = 0;
        $sql = "SELECT sum(amount) FROM " . $this->table_name;
        $sql .= " WHERE user_id IN ({$id})";
        $sql .= " AND lotto_id = " . $lotto_id;
        $sql .= " AND rame IN (0,9)";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getBetHistoryResult ($id, $lotto_id)
    {
        $sql = "SELECT sum(amount) as amount,sum(b1) as b1,sum(a1) as a1 FROM " . $this->table_name;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND lotto_id = " . $lotto_id;
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getLastBet ($id, $lotto_id, $end = 12)
    {
        $reArr = array();
        $sql = "SELECT * FROM " . $this->table_name;
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id = {$lotto_id}";
        $sql .= " AND rame IN (0,9)";
        $sql .= " ORDER BY createtime DESC, bettype, numbers";
        $sql .= " LIMIT 0," . $end;
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $reArr[] = $row;
        }
        if (0 < count($reArr))
        {
            return $reArr;
        }
        return false;
    }

    public function getSumNumberAmount ($id, $lotto_id, $bettype, $number)
    {
        $reVal = 0;
        $sql = "SELECT sum(amount) FROM " . $this->table_name;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND bettype = " . $bettype;
        $sql .= " AND numbers = '" . $number . "'";
        $sql .= " AND lotto_id = " . $lotto_id;
        $sql .= " AND rame = 0";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumBettypeAmount ($id, $lotto_id, $bettype)
    {
        $reVal = 0;
        $sql = "SELECT sum(amount) FROM " . $this->table_name;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND bettype = " . $bettype;
        $sql .= " AND lotto_id = " . $lotto_id;
        $sql .= " AND rame = 0";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getMemBetList ($bettype, $num, $page, $ids, $timeStart, $timeEnd)
    {
        $rs = new RecordSet();
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM " . $this->table_name . " a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE a.bettype=" . $bettype;
        $sql .= " AND a.numbers='" . $num . "'";
        $sql .= " AND c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        $sql .= " AND user_id IN (" . $ids . ")";
        $sql .= " ORDER BY a.createtime DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getMemBetGList ($bettype, $num, $page, $ids, $timeStart, $timeEnd)
    {
        $rs = new RecordSet();
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM " . $this->table_name . " a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE a.bettype=" . $bettype;
        $sql .= " AND a.numbers LIKE '%" . $num . "%'";
        $sql .= " AND c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        $sql .= " AND user_id IN (" . $ids . ")";
        $sql .= " ORDER BY a.createtime DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getUserSumNumberAmount ($idstr, $bettype, $number, $timeStart, $timeEnd)
    {
        $reVal = false;
        $sql = "SELECT sum(amount) as amount, sum(b1) as b1, sum(a1) as a1  FROM " . $this->table_name;
        $sql .= " WHERE user_id IN (" . $idstr . ")";
        $sql .= " AND bettype = " . $bettype;
        $sql .= " AND numbers = '" . $number . "'";
        $sql .= " AND createtime BETWEEN " . $timeStart . " AND " . $timeEnd;
        $sql .= " AND rame = 0";
        $sql .= " LIMIT 0,1";
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
        return $reVal;
    }

    public function dbInsert ($fields)
    {
        $sql = "INSERT INTO {$this->table_name} (";
        $sql .= implode(",", array_keys($fields));
        $sql .= ") VALUES (";
        $sql .= implode(",", array_values($fields));
        $sql .= ")";
        if (!$this->db->query($sql))
        {
            return false;
        }
        return mysql_insert_id($this->db->handle_master);
    }

    public function getMyMemberIdByAcl ($id, $acl)
    {
        $reVal = "";
        switch ($acl)
        {
            case "255" :
                $sql = "SELECT id FROM k_user WHERE acl = '1'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $reVal = implode(",", $idArr);
                break;
            case "5" :
                $sql = "SELECT id FROM k_user WHERE acl = '4' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id4str = implode(",", $idArr);
                if ($id4str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '3' AND prev_id IN(" . $id4str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id3str = implode(",", $idArr);
                if ($id3str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id IN (" . $id3str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "4" :
                $sql = "SELECT id FROM k_user WHERE acl = '3' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id3str = implode(",", $idArr);
                if ($id3str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id IN (" . $id3str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "3" :
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "2" :
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
        }
        return $reVal;
    }

}

class Bet2_3s extends Bet2_3d
{

    public function getSumAmount ($id, $lotto_id, $paperOpen = "")
    {
        $reVal = 0;
        $sql = "!!SELECT sum(a.amount) FROM bet2_3s1 a";
        $sql .= " LEFT JOIN lotto_3s1 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) FROM bet2_3s2 a";
        $sql .= " LEFT JOIN lotto_3s2 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) FROM bet2_3s3 a";
        $sql .= " LEFT JOIN lotto_3s3 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) FROM bet2_3s4 a";
        $sql .= " LEFT JOIN lotto_3s4 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) FROM bet2_5h a";
        $sql .= " LEFT JOIN lotto_5h b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) FROM bet2_8h a";
        $sql .= " LEFT JOIN lotto_8h b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        return $reVal;
    }

}

?>